package cn.roadtrip.mtravelbusinessmodule.bs.shopproduct;

import cn.roadtrip.utilmodule.util.DataBean;
import cn.roadtrip.utilmodule.util.enumpck.Enum_TicketRangeType;
import lombok.*;
import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.stream.Collectors;

import static cn.roadtrip.utilmodule.util.DataBean.err;
import static cn.roadtrip.utilmodule.util.DataBean.suc;

/**
 * 用户券得使用
 */
@Component
public class UserTicketImpl {

    @Autowired
    private DSLContext dslContext;


    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class Model_getUserTicketListBy {
        private String userId;
        private String productId;
        private Enum_TicketRangeType type;
    }

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    @EqualsAndHashCode
    public static class VPO_getUserTicketListBy {
        private java.lang.String userTicketId;

        public java.lang.String getUserTicketId() {
            return this.userTicketId;
        }

        private java.lang.String userTicketCreateTime;

        public java.lang.String getUserTicketCreateTime() {
            return this.userTicketCreateTime;
        }

        private java.sql.Timestamp ticketExpireTime;

        public long getTicketExpireTime() {
            return this.ticketExpireTime.getTime();
        }

        private java.lang.String ticketUseStats;

        public java.lang.String getTicketUseStats() {
            return this.ticketUseStats;
        }

        private java.math.BigDecimal ticketPrice;

        public java.math.BigDecimal getTicketPrice() {
            return this.ticketPrice;
        }

        private java.lang.String ticketType;

        public java.lang.String getTicketType() {
            return this.ticketType;
        }

        private java.lang.String ticketUseType;

        public java.lang.String getTicketUseType() {
            return this.ticketUseType;
        }

        private java.lang.String productId;

        public java.lang.String getProductId() {
            return this.productId;
        }
    }

    /**
     * 获取用户券
     *
     * @param model
     * @return
     */
    public DataBean getUserTicketListBy(
            Model_getUserTicketListBy model
    ) {
        try {
            return dslContext.transactionResult(ctx -> {
                //todo
                java.util.List<VPO_getUserTicketListBy> list = dslContext.resultQuery(
                        "SELECT\n" +
                                "couple_user_discount_coupon.id as userTicketId,\n" +
                                "couple_user_discount_coupon.create_by as userTicketCreateTime,\n" +
                                "couple_user_discount_coupon.expiration_time as ticketExpireTime,\n" +
                                "couple_user_discount_coupon.use_status as ticketUseStats,\n" +
                                "couple_user_discount_coupon.price as ticketPrice,\n" +
                                "couple_discount_coupon.coupon_type as ticketType,\n" +
                                "couple_discount_coupon.use_type as ticketUseType,\n" +
                                "couple_discount_coupon.pdt_id as productId\n" +
                                "\n" +
                                "FROM\n" +
                                "(\n" +
                                "SELECT\n" +
                                "couple_user_discount_coupon.id\n" +
                                "FROM\n" +
                                "couple_user_discount_coupon\n" +
                                "WHERE\n" +
                                "couple_user_discount_coupon.user_id = 1\n" +
                                ") as t\n" +
                                "LEFT JOIN couple_user_discount_coupon\n" +
                                "on couple_user_discount_coupon.id = t.id\n" +
                                "LEFT JOIN couple_discount_coupon\n" +
                                "on couple_discount_coupon.id = couple_user_discount_coupon.coupon_id"

                ).fetchInto(VPO_getUserTicketListBy.class);
                if (list == null || list.isEmpty()) {
                    return DataBean.sucEmpty();
                }

                list = list.stream().filter(o -> {
                    if (model.getType() != null) {
                        if (!o.getTicketType().equals(model.getType().getCode())) {
                            return false;
                        }
                    }
                    if (o.getTicketType().equals(Enum_TicketRangeType.All.getCode())) {
                        return true;
                    }
                    if (model.getProductId() != null) {
                        if (!model.getProductId().equals(o.getProductId())) {
                            return false;
                        }
                    }
                    return true;
                }).sorted(Comparator.comparing(VPO_getUserTicketListBy::getUserTicketCreateTime).reversed()).collect(Collectors.toList());
                if (list == null || list.isEmpty()) {
                    return DataBean.sucEmpty();
                }

                dslContext.commit();
                return suc(list, list.size(), "suc", "操作成功");
            });
        } catch (Exception e) {
            e.printStackTrace();
            return err("trans-err", e.getMessage());
        }
    }
}
